import React, { Component } from "react";
import SideBarUnit from "./sideBarUnit"
import "../../../css/Sidebar.css";
import urlHook from "pages/ruijing/common/utils/urlHook";
import CookieUtils from "pages/ruijing/common/utils/CookieUtils";

console.log("subBar.js");

function subBar(props) {
    const {tabNames,tabPermission,sideBarName,permissions,showGroup,selectedGroup} = props;
    const [menuStatus, updateMenuStatus] = React.useState({
        showGroup:showGroup,
        selectedGroup:selectedGroup,
        permissions:permissions
    });
    const orgId = CookieUtils.getCookie("orgId");

    //更新视图数据 私有的方法前缀用_
    function _updateData(value) {
        let newValue = { ...menuStatus, ...value };
        updateMenuStatus(newValue);
        //(updateMenuStatus(newValue),() =>renderMenu())

    }

    function updateSelected(selectGroup){
        _updateData(
            (menuStatus.selectedGroup = selectGroup)
        )
    }

    //自动登出方法
    function logout() {
        ZenUI.ST("请先登录", 2);
        CookieUtils.clearAllCookiesAndStorage();
        setTimeout(() => {
            Uts.routeToDomain('hms','');
        }, 500);
    }

    React.useEffect(() => {
        console.log("refresh view");
    });
    if(orgId&&orgId!=undefined&&orgId!=null&&permissions&&permissions.length>0&&permissions.indexOf("HMS用户")>-1) {
        return (
            <div className="zen_f-left zen_il-bl">
                <ul className="zen_w-180 zen_elli zen_p-t-30" style={{"minHeight": "600px", "height": "1200px",backgroundColor:"#eaedf1"}}>
                    <li className="zen_w-180 zen_h-30 zen_l-h-40 zen_h-40">
                        <div className="zen_t-l zen_m-l-20 zen_fs-14">{sideBarName}</div>
                    </li>
                    {tabNames.map((item, index) => (
                        SinglePermissionTab(
                            0,
                            item,
                            tabPermission[index],
                            "/systemSet",
                            menuStatus,
                            updateSelected,
                            "subNav-icon",
                            "subNav-icon-active")
                    ))}
                </ul>
            </div>
        );
    }else{
        return ""
        //logout()
    }
}

function SinglePermissionTab(tabid,name,tabPermission,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    if(menuStatus.permissions.indexOf(tabPermission)>-1){
        return <SideBarUnit
            key = {tabid}
            name = {name}
            taburl = {taburl}
            tabid = {tabid}
            updateMenu = {updataFunction}
            menuStatus = {menuStatus}
            iconClass = {iconClass}
            iconActiveClass = {iconActiveClass}
        />
    }
}

export default subBar;
